<?php
include_once dirname(__FILE__)."/../../model/config.php";
		$_ens_ue=new ens_ue();
if(isset($_POST["operation"])){
if($_POST["operation"]=="INSERT"){
		if(isset($_POST["code_ens"])){$_ens_ue->set_code_ens($_POST["code_ens"]);}
		if(isset($_POST["code_ue"])){$_ens_ue->set_code_ue($_POST["code_ue"]);}
		if(isset($_POST["role"])){$_ens_ue->set_role($_POST["role"]);}
		if(isset($_POST["code_etab_rattach"])){$_ens_ue->set_code_etab_rattach($_POST["code_etab_rattach"]);}
		if(isset($_POST["nbr_ens"])){$_ens_ue->set_nbr_ens($_POST["nbr_ens"]);}
		$res=$_ens_ue->INSERT(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
}
elseif($_POST["operation"]=="DELETE"){
		if(isset($_POST[""])){
		$_ens_ue->set_($_POST[""]);
		$res=$_ens_ue->DELETE(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
		}
}
elseif($_POST["operation"]=="UPDATE"){
		if(isset($_POST["code_ens"])){$_ens_ue->set_code_ens($_POST["code_ens"]);}
		if(isset($_POST["code_ue"])){$_ens_ue->set_code_ue($_POST["code_ue"]);}
		if(isset($_POST["role"])){$_ens_ue->set_role($_POST["role"]);}
		if(isset($_POST["code_etab_rattach"])){$_ens_ue->set_code_etab_rattach($_POST["code_etab_rattach"]);}
		if(isset($_POST["nbr_ens"])){$_ens_ue->set_nbr_ens($_POST["nbr_ens"]);}
		$res=$_ens_ue->UPDATE(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
}

}
else{
	$constraint='';$limit=0;$start=0;$pagination='';
	if(isset($_POST["code_ens"]) && !empty($_POST["code_ens"])){
		$constraint.=" AND code_ens='".$_POST["code_ens"]."'";
	}

	if(isset($_POST["code_ue"]) && !empty($_POST["code_ue"])){
		$constraint.=" AND code_ue='".$_POST["code_ue"]."'";
	}

	if(isset($_POST["role"]) && !empty($_POST["role"])){
		$constraint.=" AND role='".$_POST["role"]."'";
	}

	if(isset($_POST["code_etab_rattach"]) && !empty($_POST["code_etab_rattach"])){
		$constraint.=" AND code_etab_rattach='".$_POST["code_etab_rattach"]."'";
	}

	if(isset($_POST["nbr_ens"]) && !empty($_POST["nbr_ens"])){
		$constraint.=" AND nbr_ens='".$_POST["nbr_ens"]."'";
	}

	if(isset($_POST['limit'])){$limit=$_POST['limit'];}
	if(isset($_POST['start'])){$start=$_POST['start'];}
	if($limit>0){
		$pagination.=" LIMIT  $start,$limit"; 
}
		$res=$_ens_ue->SELECT($constraint,$pagination); 

		 $data=array();
		  while($row=$_ens_ue->fetch_object()){
		  	$data[]=$row;
	  	  }
  	 	 
		
	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('totalcount' => count($data),'result' => $data ));
	  	
	  }}
?>